<template>
    <div class="fr page">
        <div class="sui-pagination">
            <button type="button" :disabled="pageNo == 1" @click="$emit('getPageNo', pageNo - 1)">
                <i class="iconfont icon-left"></i><span>上一页</span>
            </button>
            <div class="page-list">
                <button type="button" :class="{ active: pageNo == 1 }" v-if="startNumAndEndNum.start > 1"
                    @click="$emit('getPageNo', 1)">
                    <span>1</span>
                </button>
                <button type="button" v-if="startNumAndEndNum.start > 2"><span>...</span></button>
                <button type="button" :class="{ active: pageNo == page }" v-for="(page, index) in startNumAndEndNum.end"
                    :key="index" v-if="page >= startNumAndEndNum.start" @click="$emit('getPageNo', page)">
                    <span>{{ page }}</span>
                </button>
                <button type="button" v-if="startNumAndEndNum.end < totalPage - 1"><span>...</span></button>
                <button type="button" :class="{ active: pageNo == totalPage }" v-if="startNumAndEndNum.end < totalPage"
                    @click="$emit('getPageNo', totalPage)">
                    <span>{{ totalPage }}</span>
                </button>
            </div>
            <button type="button" :disabled="pageNo == totalPage" @click="$emit('getPageNo', pageNo + 1)">
                <span>下一页</span><i class="iconfont icon-right"></i>
            </button>
            <button type="button" class="page-total"><span>共{{ total }}条</span></button>
            <div></div>
        </div>
    </div>
</template>

<script>
export default {
    name: "Pagination",
    props: ["pageNo", "pageSize", "total", "continues"],
    computed: {
        totalPage() {
            // 总共多少页
            // 向上取整
            return Math.ceil(this.total / this.pageSize);
        },
        // 计算出连续的页码
        startNumAndEndNum() {
            const { continues, pageNo, totalPage } = this;
            let start = 0, end = 0;
            if (continues > totalPage) {
                start = 1;
                end = totalPage;
            } else {
                start = pageNo - parseInt(continues / 2);
                end = pageNo + parseInt(continues / 2);

                // start出现负数
                if (start < 1) {
                    start = 1;
                    end = continues;
                }

                // end数字大于总页数
                if (end > totalPage) {
                    end = totalPage;
                    start = totalPage - continues + 1;
                }
            }
            return { start, end };
        }
    }
}
</script>

<style scoped lang="less">
.page {
    display: flex;
    justify-content: right;

    .sui-pagination {
        margin: 18px 0;

        button {
            padding: 0 10px;
            height: 30px;
            font-size: 14px;
            border-width: 1px;
            border-radius: 6px;
            border-style: solid;
            border-color: #dddddd;
            background: #FFFFFF;
            color: #111111;
            cursor: pointer;
            outline: none;

            &>i,
            span {
                vertical-align: middle;
            }

            &.active {
                border-color: #ff5f00;
                background: #ff5f00;
                color: #FFFFFF;
                box-shadow: none;
            }
        }

        .page-list {
            display: inline-block;
            vertical-align: top;
            margin: 0 5px;

            &>button {
                margin: 0 5px;
            }
        }

        .page-total {
            margin-left: 5px;
        }
    }
}
</style>